<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-28上午8:53:00
 *
 */
 
 /**
  * CanGoo Blog Post 控制器
  * 
  * @package blog
  * @subpackage controllers
  * 
  */
  class Article extends CI_Controller{
  	
  	/**
  	 * 构造函数
  	 */
  	public function __construct(){
  		parent::__construct();
  		$this->load->library('blog/option');
  		$this->load->library('blog/post');
  		$this->load->library('blog/comment');
  		$this->load->library('session');
  		
  		$this->load->model('blog/post_model');
  		
  		$this->load->helper('url');
  		$this->load->helper('common');
  		$this->load->library('blog/widget');
  		$this->load->library('user_agent');
  		$this->load->helper('date');
  		$this->load->model('blog/comment_model');
  		
  		$this->load->library('form_validation');
  		//验证规则
  		$this->form_validation->set_rules('reply_name', '昵称', 'trim|required|min_length[2]|max_length[20]|xss_clear');
  		$this->form_validation->set_rules('reply_email', '邮件', 'trim|required|min_length[6]|max_length[20]|valid_email|xss_clear');
  		$this->form_validation->set_rules('reply_url', '网址', 'trim|required|min_length[2]|max_length[20]|prep_url|xss_clear');
  		$this->form_validation->set_rules('reply_text', '留言内容', 'trim|required|min_length[5]|xss_clear');
  		
  	}
  	
  	/**
  	 * 默认方法
  	 */
  	public function index($pid){

  		/**
  		 * 获取标题
  		 */
  		$row = $this->post_model->get_one(array('title'), 'blog_post', array('bpid'=>$pid));
  		$data['title'] = $row->title;
  		
  		$this->post->__set('current_pid', $pid);
  		$this->post->__set('content_type', 'article');
	    $this->post->run();
  		
  		$this->comment->__set('current_pid', $pid);
  		
  		$this->load->view('blog/article_view', $data);
  		$this->_update_view($pid);
  	}
  	
  	public function comment_add(){
  		
  		header('Content-type: application/json; charset=UTF-8');
  		if($this->form_validation->run() == TRUE){
	  		$data['parent'] = $this->input->post('reply_parent');
//	  		$data['uid'] = empty($this->session->userdata('uid'))?0:$this->session->userdata('uid');
			$data['uid'] = 7;
	  		$data['pid'] = $this->input->post('reply_pid');
	  		$data['text'] = $this->input->post('reply_text');
	  		$data['status'] = 'verifying';
	  		$user = array('name'=>$this->input->post('reply_name'), 'email'=>$this->input->post('reply_email'), 'url'=>$this->input->post('reply_url'));
	  		$data['userData'] = serialize($user);
	  		$data['userAgent'] = $this->agent->agent_string();
	  		$data['addTime'] = $data['modTime'] = now();
	  		$cid = $this->comment_model->insert('comment', $data);
	  		
	  		$ajax['date'] = mdate('%Y-%m-%d %H:%i:%s', $data['addTime']);
	  		$ajax['cid'] = $cid;
	  		echo json_encode($ajax);   			
  		}else{
  			echo validation_errors().'#'.set_value('reply_name').'#'.set_value('reply_url');
  		}
  	}
  	
  	/**
  	 * 当载入一篇文章时，要更新文章的blog-post中的viewNum字段
  	 */
  	private function _update_view($bpid){
  		if($bpid != 0){
			$this->post_model->update_view($bpid);  			
  		}
  	}
  }

/**/
